package com.mes.service.manu;

import com.mes.common.MesPdaConstant;
import com.yy.base.common.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FlyWeavService {
    @Autowired
    private Dao dao;

    //加载飞织作业信息
    public List<Map<String,Object>> selectFlyWeavList(Map map) {
        String sql ="select o.emeso_job_order_id id, o.code, p.name, f.name fzj, f.Position, " +
                "       oe.exe_date_from, oe.exe_date_to, oe.status," +
                "       (select mo.foot_size_code from emeso_manuorder mo where mo.emeso_manuorder_id = o.emeso_manuorder_id) size_code," +
                "\t\t\t mes_get_xm_color(o.emeso_manuorder_id) colorname, o.status_xm " +
                "  from emes_process pr, eam_facilities f, m_product p, emeso_job_order o, Emeso_Job_Order_Exe oe\n" +
                " where p.m_product_id = o.m_product_id\n" +
                "   and f.eam_facilities_id = oe.eam_facilities_id\n" +
                "   and pr.emes_process_id = oe.emes_process_id\n" +
                "   and oe.emeso_job_order_id = o.emeso_job_order_id\n" +
                "   and oe.status = 'FINISHED'\n" +
                "   and pr.Route_No = " + MesPdaConstant.FLYWEAVCODE;

        if(map.get("status") != null && !"".equals(map.get("status"))){
            sql += " and nvl(o.status_xm, '') = '" + map.get("status") + "'";
        } else {
            sql += " and nvl(o.status_xm, '') <> 'OK'";
        }
        if(map.get("flyWeav")!= null && !"".equals(map.get("flyWeav"))){
            sql += " and oe.Eam_Facilities_ID = '"+map.get("flyWeav")+"'";
        }
        if(map.get("jobCode")!= null && !"".equals(map.get("jobCode"))){
            sql += " and o.Code like '%"+map.get("jobCode") + "%'";
        }
        return dao.query(sql);
    }

    public List<Map<String,Object>> selectFlyWeavs() {
        return dao.query("SELECT a.Eam_Facilities_Id eamFacilitiesId,a.name, a.Position " +
                "  from Eam_Facilities a where facility_type = 'FZJ' order by a.code");
    }

    public List<Map<String,Object>> selectJobStatus() {
        return dao.query("SELECT " +
                "   t.tablename, c.columnname, rl.value, rl.name, rl.seqno from ad_ref_list rl, ad_column c, ad_table t " +
                "  where rl.ad_reference_id = c.ad_reference_value_id " +
                "   and c.ad_table_id = t.ad_table_id " +
                "   and upper(t.tablename) = upper('Emeso_Job_Order_Exe') " +
                "   and upper(c.columnname) = upper('Status')" +
                "   order by t.tablename, c.columnname, rl.seqno");
    }

    //将作业工单的鞋面写入RFID设为已绑定
    public boolean UpdateJobMaterialok(String code) {
        String sql =  " update Emeso_Job_Order set uppers_ok = 'Y', status_xm = 'WRITER' where  code = ? ";
        return dao.executeSql(sql, code) > 0;
    }

    //修改鞋面状态
    public String getXmStatus(String code) {
        String sql =  "select status_xm from Emeso_Job_Order where  code = ? ";
        Map<String, Object> result =  dao.queryUnique(sql, code);
        if (result != null) {
            return (String)result.get("status_xm");
        }
        return null;
    }

    //修改鞋面状态
    public boolean updateXmStatus(String code, String status) {
        String sql =  " update Emeso_Job_Order set status_xm = ? where  code = ? ";
        return dao.executeSql(sql, status, code) > 0;
    }
}
